//class that represents a binary LFSR
//the maximum length of the LFSR is 32-bits
//The implementation uses the following formula to create a new bit
//(this will explain the feedback notation)
//sL=state[L-1]poly[L-1]+state[L-2]poly[L-2]+ ... + state[0]poly[0]

#include "lfsr32.h"
 
//step the LFSR one time
int lfsr32::clock(){

  //calculate new bit as weight of state and poly
  word32 temp(state);
  temp.andOp(poly);
  int newBit=(temp.weight()&1);
  //take care of old bit, shift and insert new bit 
  int output=state.getBit(0);
  state.rshift(1);
  if(newBit==1)
    state.setBit(L-1); 
  return output;
}
  
  //step the LFSR 32 times
word32 lfsr32::clock32(){
  word32 output;
  word32 temp;
  int newBit;
  for(int i=0;i<32;i++){
    //use the same principle as for clock
    //calculate new Bit
    temp.assign(state);
    temp.andOp(poly);
    newBit=(temp.weight()&1);
    //save ouput bit in output word
    output.setVal(i,state.getBit(0));
    //shift and update state with new bit
    state.rshift(1);
    if(newBit==1)
      state.setBit(L-1); 
  }
  return output;
}
